/**
 * @file sync_bn_training_update.cpp
 *
 * Copyright (C) 2024. Huawei Technologies Co., Ltd. All rights reserved.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
#include "kernel_operator.h"



constexpr int32_t TOTAL_LENGTH =  8 * 2048;
constexpr int32_t USE_CORE_NUM = 8;
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;
constexpr int32_t TILE_NUM = 8;
constexpr int32_t BUFFER_NUM = 2;
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM;

// 定义复数类型
struct complex64 {
    float real;
    float imag;
};

// 定义复数类型
struct complex128 {
    double real;
    double imag;
};

template<typename T>
class KernelIsReal {
public:
    __aicore__ inline KernelIsReal() {}

    __aicore__ inline void Init(GM_ADDR xAddr, GM_ADDR zAddr, int32_t dtype)
    {
        this->dtype_ = dtype;
        xGm.SetGlobalBuffer((__gm__ T*)xAddr + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        zGm.SetGlobalBuffer((__gm__ int8_t*)zAddr + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);

        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(T));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(int8_t));
    }

    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<T> xLocal = inQueueX.AllocTensor<T>();
        AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        inQueueX.EnQue(xLocal);
    }

    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<T> xLocal = inQueueX.DeQue<T>();
        AscendC::LocalTensor<int8_t> zLocal = outQueueZ.AllocTensor<int8_t>();

        bool isComplex = (dtype_ == 16 || dtype_ == 17);

        for (int i = 0; i < TILE_LENGTH; ++i) {

            zLocal.SetValue(i, isComplex ? 0 : 1);
        }

        outQueueZ.EnQue(zLocal);
        inQueueX.FreeTensor(xLocal);
        
    }

    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<int8_t> zLocal = outQueueZ.DeQue<int8_t>();
        AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<T> xGm;
    AscendC::GlobalTensor<int8_t> zGm;
    int32_t dtype_;
};

// 核函数定义
extern "C" __global__ __aicore__ void is_real_custom(GM_ADDR x, GM_ADDR z, int32_t dtype)
{
    switch (dtype) {
        case 1: {
            KernelIsReal<half> op;
            op.Init(x, z, dtype);
            op.Process();
            break;
        }
        case 0: {
            KernelIsReal<float> op;
            op.Init(x, z, dtype);
            op.Process();
            break;
        }
        case 16: {
            KernelIsReal<complex64> op;
            op.Init(x, z, dtype);
            op.Process();
            break;
        }
        case 17: {
            KernelIsReal<complex128> op;
            op.Init(x, z, dtype);
            op.Process();
            break;
        }
        default: {
            // 不支持类型
            break;
        }
    }
}


#ifndef ASCENDC_CPU_DEBUG
void is_real_do(uint32_t blockDim, void *stream, void* x, void* z, int32_t dtype)
{
    is_real_custom<<<blockDim, nullptr, stream>>>( x , z,dtype);
}
#endif
